package cn.pzhu.controller.web;

import cn.pzhu.controller.BaseController;
import cn.pzhu.model.*;
import cn.pzhu.services.web.*;
import com.jfinal.upload.UploadFile;

import java.util.List;

/**
 * Created by dongp on 2016/5/14.
 */
public class WebMemberController extends BaseController{

	private WebMemberServices webMemberServices = new WebMemberServices();
	private WebOrderServices orderServices = new WebOrderServices();
	private WebAcheievementServices acheievementServices = new WebAcheievementServices();
	private WebCategoryServices categoryServices = new WebCategoryServices();
	private WebDemandServices demandServices = new WebDemandServices();

	public void myMember(){
		User user = (User) getSession().getAttribute("user");
		if(user == null) {
			redirect("/web/login/login");
			return;
		}

		render("mycenter/memberApply.jsp");
	}

	public void memberApply(){
		User user = (User) getSession().getAttribute("user");
		if(user == null) {
			redirect("/web/login/login");
			return;
		}

		int userID = user.getInt("userID").intValue();
		String email = getPara("email", "");
		String phone = getPara("phone","");
		String postCode = getPara("postCode","");
		String identity = getPara("identity","");
		String address = getPara("address","");
		String unitName = getPara("unitName","");
		String profitYear = getPara("profitYear","");
		String techArea = getPara("techArea","");
		String profile = getPara("profile","");
		boolean result = webMemberServices.memberApply(userID,email,phone,postCode,identity,address,unitName,profitYear,techArea,profile);
		if(result) {
			setAttr("msg","会员身份审核提交成功");
		}
		render("mycenter/memberApply.jsp");
	}

	public void queryMember(){
		User user = (User) getSession().getAttribute("user");
		if(user == null) {
			redirect("/web/login/login");
			return;
		}

		int userID = user.getInt("userID").intValue();
		Member member = webMemberServices.queryMember(userID);
		setAttr("member",member);
		render("mycenter/memberQuery.jsp");
	}

	public void queryOrder(){
		User user = (User) getSession().getAttribute("user");
		if(user == null) {
			redirect("/web/login/login");
			return;
		}

		int userID = user.getInt("userID").intValue();
		List<Order> orderList = orderServices.queryOrder(userID);
		setAttr("orderList",orderList);
		render("mycenter/order.jsp");
	}

	/**
	 * 查询技术成果
	 */
	public void queryAchievement(){
		User user = (User) getSession().getAttribute("user");
		if(user == null) {
			redirect("/web/login/login");
			return;
		}

		int userID = user.getInt("userID").intValue();
		List<Achievement> achievementList =  acheievementServices.queryByUserId(userID);
		setAttr("achievementList",achievementList);
		render("mycenter/acheievementQuery.jsp");
	}

	public void queryAchievementForModify(){
		User user = (User) getSession().getAttribute("user");
		if(user == null) {
			redirect("/web/login/login");
			return;
		}

		int id = getParaToInt("id");
		Achievement achievement =  acheievementServices.queryDetail(id);
		setAttr("achievement",achievement);
		List<Category> categoryList = categoryServices.queryList();
		setAttr("categoryList", categoryList);
		render("mycenter/modifyAchievement.jsp");
	}

	public void delAchievement(){
		User user = (User) getSession().getAttribute("user");
		if(user == null) {
			redirect("/web/login/login");
			return;
		}

		int id = getParaToInt("id");
		boolean rst =  acheievementServices.delAchievement(id);
		redirect("/web/member/queryAchievement");
	}

	public void queryDemand(){
		User user = (User) getSession().getAttribute("user");
		if(user == null) {
			redirect("/web/login/login");
			return;
		}

		int userID = user.getInt("userID").intValue();
		List<Demand> demandList =  demandServices.queryByUserId(userID);
		setAttr("demandList",demandList);
		render("mycenter/demandQuery.jsp");
	}

	public void delDemand(){
		User user = (User) getSession().getAttribute("user");
		if(user == null) {
			redirect("/web/login/login");
			return;
		}

		int demandID = getParaToInt("demandID");
		int userID = user.getInt("userID").intValue();
		boolean rst =  demandServices.delDemand(demandID);
		redirect("/web/member/queryDemand");
	}

	public void issueAchievement() {
		User user = (User) getSession().getAttribute("user");
		if(user == null) {
			redirect("/web/login/login");
			return;
		}

		List<Category> categoryList = categoryServices.queryList();
		setAttr("categoryList", categoryList);
		render("mycenter/issueAchievement.jsp");
	}

	public void addAchievement() {
		User user = (User) getSession().getAttribute("user");
		if(user == null) {
			redirect("/web/login/login");
			return;
		}

		int userID = user.getInt("userID").intValue();
		Member member = webMemberServices.queryMember(userID);
		int status = member.getInt("status").intValue();
		if(status != 3) {  //3 代表审核成为会员
			setAttr("msg","你还不是会员，不能发布技术成果!");
			render("mycenter/issueAchievement.jsp");
			return;
		}

		// 上传文件
		UploadFile file = getFile("imgeurl","");
		String fileName = "";
		if(file != null) {
			fileName = file.getFileName();
		}

		String imgeurl = "upload/"+fileName;
		String name = getPara("name");
		int categoryID = getParaToInt("categoryID");
		String level = getPara("level");
		String isPat = getPara("isPat");
		String tradeMode = getPara("tradeMode");
		String provideInfo = getPara("provideInfo");
		String phone = getPara("phone");
		String address = getPara("address");
		String serverPromise = getPara("serverPromise");
		String desc = getPara("desc");
		double money = Double.parseDouble(getPara("money"));
		String rank = getPara("rank");
		String tel = getPara("tel");
		double rebate = Double.parseDouble(getPara("rebate"));
		boolean result = acheievementServices.addAchievement(userID,categoryID,name,level,imgeurl,isPat,desc,tradeMode,
				serverPromise,provideInfo,phone,address,money,rank,tel,rebate);

		if(result) {
			setAttr("msg","技术成果已经发布成功，等待审核");
		}else {
			setAttr("msg","技术成果发布失败，请重新发布");
		}
		render("mycenter/issueAchievement.jsp");
	}

	public void modifyAchievement() {
		User user = (User) getSession().getAttribute("user");
		if(user == null) {
			redirect("/web/login/login");
			return;
		}

		int userID = user.getInt("userID").intValue();
		Member member = webMemberServices.queryMember(userID);
		int status = member.getInt("status").intValue();
		if(status != 3) {  //3 代表审核成为会员
			setAttr("msg","你还不是会员，不能发布技术成果!");
			render("mycenter/issueAchievement.jsp");
			return;
		}

		int id = getParaToInt("id");
		String name = getPara("name");
		int categoryID = getParaToInt("categoryID");
		String level = getPara("level");
		String isPat = getPara("isPat");
		String tradeMode = getPara("tradeMode");
		String provideInfo = getPara("provideInfo");
		String phone = getPara("phone");
		String address = getPara("address");
		String serverPromise = getPara("serverPromise");
		String desc = getPara("desc");
		double money = Double.parseDouble(getPara("money"));
		String rank = getPara("rank");
		String tel = getPara("tel");
		double rebate = Double.parseDouble(getPara("rebate"));
		boolean result = acheievementServices.modifyAchievement(id, userID, categoryID, name, level, isPat, desc, tradeMode,
				serverPromise, provideInfo, phone, address, money, rank, tel, rebate);

		if(result) {
			setAttr("msg","技术成果修改成功,等待审核");
		}else {
			setAttr("msg","技术成果修改失败");
		}
		redirect("/web/member/queryAchievement");
	}

	public void issueDemand() {
		User user = (User) getSession().getAttribute("user");
		if(user == null) {
			redirect("/web/login/login");
			return;
		}

		List<Category> categoryList = categoryServices.queryList();
		setAttr("categoryList", categoryList);
		render("mycenter/issueDemand.jsp");
	}

	public void addDemand() {
		User user = (User) getSession().getAttribute("user");
		if(user == null) {
			redirect("/web/login/login");
			return;
		}

		int userID = user.getInt("userID").intValue();
		Member member = webMemberServices.queryMember(userID);
		int status = member.getInt("status").intValue();
		if(status != 3) {  //3 代表审核成为会员
			setAttr("msg","你还不是会员，不能发布技术需求!");
			render("mycenter/issueDemand.jsp");
			return;
		}


		String name = getPara("name");
		int categoryID = getParaToInt("categoryID");
		double budget = Double.parseDouble(getPara("budget"));
		String demandDesc = getPara("demandDesc");
		String organization = getPara("organization");
		String phone = getPara("phone");
		String address = getPara("address");

		boolean result = acheievementServices.addDemand(userID,categoryID,name,budget,demandDesc,organization,phone,address);

		if(result) {
			setAttr("msg","技术需求已经发布成功，等待审核");
		}else {
			setAttr("msg","技术需求发布失败，请重新发布");
		}
		render("mycenter/issueDemand.jsp");
	}


}
